package stationGUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JTextPane;

public class ColorChooserScreen extends JPanel {
  /**
   * 
   */
  private static final long serialVersionUID = 1L;
  
  //holds the info of the selection boxes
  public static final ColorChooser[] COLORS = {new ColorChooser("blue",Color.BLUE),
                                              new ColorChooser("green",Color.GREEN),
                                              new ColorChooser("red",Color.RED),
                                              new ColorChooser("yellow",Color.YELLOW)};
  public static final Font FONT = new Font("Times New Roman",Font.BOLD,24);
  
  //logistic vars
  int midw, midh;
  private Toolkit toolkit;
  Dimension size;
  FontMetrics fm;
  int fh, fw;
  
  JTextPane text;
  ClientNode clientNode;
  
  
  //TODO LOOK OUT FOR THIS FINAL VARIABLE MODIFIER!!!!
  public ColorChooserScreen(ClientNode n) {
    setBackground(Station.BACKGROUND_COLOR);
    
    //set my prefered what's left after a sqaure is taken out
    toolkit = Toolkit.getDefaultToolkit();
    size = toolkit.getScreenSize();
    size.width = size.width-size.height;
    setPreferredSize(size);
    
    //set the font info
    fm = getFontMetrics(FONT);
    fh=fm.getHeight();
    
    //set the client node for this neighborhood screen
    clientNode=n;
    
    //add the necessary mouse listener
    addMouseListener(new MouseAdapter(){
      public void mousePressed(MouseEvent e){
        //get the mouse position
        int xPos=e.getX();
        int yPos=e.getY();
        
        for(ColorChooser iter:COLORS) {
          //get the ColorChooser position and check if they are the same
          double x=iter.r.x;
          double y=iter.r.y;
          if(xPos>x && xPos<x+iter.r.width && yPos>y && yPos<y+iter.r.height)
            try {

              clientNode.me.color=iter.color;
              repaint();
              clientNode.sendColor(iter.color);
            } catch (Exception e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            }
        }
      }
    });
  }
  
  public void paint(Graphics g2) {
    Graphics2D g = (Graphics2D)g2;
    
    //get the size of the panel
    size=this.getSize();
    midw=(size.width)/2;
    midh=(size.height)/2;
    
    //set the bounderies of my color choosers
    int tot=COLORS.length;
    for(int i=0;i<tot;i++) {
      int w=fm.stringWidth(COLORS[i].name);
      COLORS[i].set(midw-w/2,i*fh,w,fh);
    }
    
    //paint the newly updated color choosers
    g.setFont(FONT);
    for(ColorChooser iter:COLORS) {
      //TODO FIX ME!!!!!!
      iter.paintColorChooser(g,clientNode.me.color);
      //iter.paintColorChooser(g,me.color);
    }
  }
}
  
  

